package com.sf.fy.uav.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sf.fy.uav.common.enums.PricingRuleTypeEnum;
import com.sf.fy.uav.common.request.pricing.PricingAddRequest;
import com.sf.fy.uav.common.utils.BeanUtil;
import com.sf.fy.uav.order.entity.PricingRuleRange;
import com.sf.fy.uav.order.mapper.PricingRuleRangeMapper;
import com.sf.fy.uav.order.service.IPricingRuleRangeService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 定价规则范围明细表 服务实现类
 * </p>
 *
 * @author LTQ
 * @since 2023-11-14
 */
@Service
public class PricingRuleRangeServiceImpl extends ServiceImpl<PricingRuleRangeMapper, PricingRuleRange> implements IPricingRuleRangeService {
    //    private static final Integer NUM_CONST = 1;
    @Autowired
    private  PricingRuleRangeMapper pricingRuleRangeMapper;
    @Override
    public void add(PricingAddRequest pricingAddRequest) {
        Date date = new Date();
//        for (PricingAddRequest.PricingRuleRange pricingRuleRange : pricingAddRequest.getPricingRuleRangeArray()) {
        if(!CollectionUtils.isEmpty(pricingAddRequest.getPricingRuleRangeArray())){
            for (int i = 0; i < pricingAddRequest.getPricingRuleRangeArray().size(); i++) {
                List<PricingRuleRange> pricingRuleRanges = BeanUtil.copyProperties(pricingAddRequest.getPricingRuleRangeArray().get(i).getWeightOrDistanceList(), PricingRuleRange.class);
                for (PricingRuleRange ruleRange : pricingRuleRanges) {
                    ruleRange.setCreateTime(date);
                    ruleRange.setUpdateTime(date);
                    ruleRange.setPricingRuleId(pricingAddRequest.getPricingRuleIds().get(i));
                }
                if(ObjectUtils.isNotEmpty(pricingAddRequest.getPricingRuleRangeArray().get(i).getType())&& (PricingRuleTypeEnum.WEIGHT.getType().equals(pricingAddRequest.getPricingRuleRangeArray().get(i).getType()) || PricingRuleTypeEnum.DISTANCE.getType().equals(pricingAddRequest.getPricingRuleRangeArray().get(i).getType()))){
                    pricingRuleRangeMapper.insertBatchSomeColumn(pricingRuleRanges);
                }

            }
        }
    }
}

